﻿/* ==============================================================================
 * 功能描述：TipsPanel  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/5/17 22:43:56
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using ZZQFrameWork;

public class TipsPanel : MonoBehaviour
{
    public GoodsVO Data { get; set; }

    /// <summary>
    /// 物品描述
    /// </summary>
    private Text desText;

    /// <summary>
    /// 物品描述
    /// </summary>
    private Text contentText;

    /// <summary>
    /// 是否处于显示状态
    /// </summary>
    private bool isShow = false;

    private CanvasGroup canvasGroup;

    /// <summary>
    /// 透明度转换速度
    /// </summary>
    public float alphaChangeSpeed = 3;

    /// <summary>
    /// 偏移量
    /// </summary>
    public Vector2 offest;

    /// <summary>
    /// 目标透明度
    /// </summary>
    private float targetAlpha;

    /// <summary>
    /// 摄像机
    /// </summary>
    private GameObject canvas;

    void Awake()
    {
        canvasGroup = GetComponent<CanvasGroup>();
        desText = GetComponent<Text>();
        contentText = transform.GetComponentByPath<Text>("Content");
        canvas = UIManager.Instance.UIContainer.parent.gameObject;
        offest=new Vector2(70,-70);
    }

    public void Show(GoodsVO goods)
    {
        isShow = true;
        targetAlpha = 1;
        string info = goods.GetInfoShowTips();
        desText.text = info;
        contentText.text = info;
    }

    public void Hide()
    {
        targetAlpha = 0;
        isShow = false;
    }

    void Update()
    {
        if (isShow)
        {
            Vector2 pos;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.GetComponent<RectTransform>()
                , Input.mousePosition, null, out pos);
            transform.localPosition = pos+offest;
        }

        if (!canvasGroup.alpha.Equals(targetAlpha))
        {
            float alpha = Mathf.Lerp(canvasGroup.alpha, targetAlpha, alphaChangeSpeed);
            canvasGroup.alpha = alpha;
            if (Mathf.Abs(canvasGroup.alpha - targetAlpha) < .02f)
            {
                canvasGroup.alpha = targetAlpha;
            }
        }
    }
}